package com.simba.server.jsonservice.serivces;

import com.simba.common.jsonserver.JsonChannelManager;
import com.simba.common.jsonservice.JsonService;
import com.simba.common.log.LogUtil;
import com.simba.server.base.CommonUtil;
import com.simba.server.components.SortCommonDefines;
import com.simba.server.components.SorterParams;
import org.apache.log4j.Logger;
import org.jboss.netty.channel.Channel;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author create by kavin
 * @date 2020/10/16 11:06
 */
public class ChangeUploadJsonService implements JsonService {
    public static final String NAME = "RepeatUpload";
    private final static Logger logger = Logger.getLogger(ChangeUploadJsonService.class);

    @Override
    public void handleRequest(Channel channel, Object content) {
        Map<String, Object> map = new HashMap<>();
        map.put(SortCommonDefines.JSON_SERVICE_NAME, NAME);
        try
        {
            JSONObject request = (JSONObject) content;
            String flag = request.getString("flag");
            SorterParams.getInstance().setRepeatUpload(flag.equals("true"));

            LogUtil.LogInfo(logger, "Current repeat upload mode:" + SorterParams.getInstance().getRepeatUpload());
            map.put("Status", true);
        }
        catch (Exception ex)
        {
            map.put("Status", false);
            LogUtil.LogError(logger, "Change repeat upload mode error " + ex.getMessage());
        }finally
        {
            String jsonObject = CommonUtil.getGson().toJson(map);
            JsonChannelManager.getInstance().commitResponse(channel, jsonObject.getBytes(), false);
        }
    }
}
